package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.NoteList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.activity.NoteDetail;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.customview.GlideCircleTransform;
import com.lsy.weizhi2.myutils.ImageAdapterCall;
import com.lsy.weizhi2.swipetoloadlayout.BaseRecyclerAdapter;
import com.lsy.weizhi2.utils.MyMethod;

import java.util.List;

/**
 * Created by 嗨哟 on 2017/6/13.
 */

public class ClassifyAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, NoteList.ListBean> {
    private Context context;
    private List<NoteList.ListBean>list_notes;

    public ClassifyAdapter(List<NoteList.ListBean> list_notes,Context context) {
        super(list_notes);
        this.list_notes=list_notes;
        this.context=context;
    }

    @Override
    public BaseRecyclerViewHolder createViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        return new HomeNotesHolder(inflater.inflate(R.layout.note, parent, false));
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder holder, int position, NoteList.ListBean data) {
        ((ClassifyAdapter.HomeNotesHolder) holder).position = position;

        String[] str = null;
        if (list_notes != null && list_notes.size() != 0) {
            String photos = list_notes.get(position).getPhoto();
            if (photos != null && photos.length() != 0) {
                str = photos.split(",");
                ((ClassifyAdapter.HomeNotesHolder) holder).total_num.setText("共" + str.length + "张");
            }
            if (str != null) {
                ((ClassifyAdapter.HomeNotesHolder) holder).imageAdapter.UpData(str);
            } else {
                ((ClassifyAdapter.HomeNotesHolder) holder).image_rv.setVisibility(View.GONE);
                ((ClassifyAdapter.HomeNotesHolder) holder).total_num.setVisibility(View.GONE);
            }
        }
        if (list_notes != null && list_notes.size() != 0) {
            ((ClassifyAdapter.HomeNotesHolder) holder).name_tv.setText(list_notes.get(position).getCreateUsername());
            ((ClassifyAdapter.HomeNotesHolder) holder).time_tv.setText(list_notes.get(position).getCreateTime());
            ((ClassifyAdapter.HomeNotesHolder) holder).title_tv.setText(list_notes.get(position).getTitle());
            int second = list_notes.get(position).getSecondCategoryId();
            ((ClassifyAdapter.HomeNotesHolder) holder).classify_tv.setText(Constants.name[second]);
            int method = list_notes.get(position).getPriceMethod();
            switch (method) {
                case 0:
                    ((ClassifyAdapter.HomeNotesHolder) holder).price_tv.setText(list_notes.get(position).getPrice() + "/次");//服务单价
                    break;
                case 1:
                    ((ClassifyAdapter.HomeNotesHolder) holder).price_tv.setText(list_notes.get(position).getPrice() + "/时");//服务单价
                    break;
                case 2:
                    ((ClassifyAdapter.HomeNotesHolder) holder).price_tv.setText(list_notes.get(position).getPrice() + "/天");//服务单价
                    break;
                case 3:
                    ((ClassifyAdapter.HomeNotesHolder) holder).price_tv.setText(list_notes.get(position).getPrice() + "/周");//服务单价
                    break;
                case 4:
                    ((ClassifyAdapter.HomeNotesHolder) holder).price_tv.setText(list_notes.get(position).getPrice() + "/月");//服务单价
                    break;
                case 5:
                    ((ClassifyAdapter.HomeNotesHolder) holder).price_tv.setText(list_notes.get(position).getPrice() + "/年");//服务单价
                    break;
            }
            ((ClassifyAdapter.HomeNotesHolder) holder).reply_number.setText(list_notes.get(position).getReplyCount() + "");
            ((ClassifyAdapter.HomeNotesHolder) holder).look_number.setText(list_notes.get(position).getClickCount() + "");
            ((ClassifyAdapter.HomeNotesHolder) holder).ok_number.setText(list_notes.get(position).getDealCount() + "");
        }
        Glide.with(context).load(new GlideUrl(Constants.DOWN_USER_PICTURE + list_notes.get(position).getAvatar(), new LazyHeaders.Builder().addHeader("WZ-AUTH-TOKEN", MyInfo.Token).addHeader("WZ-AUTH-ID", MyInfo.User_Id + "").build()))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideCircleTransform(context))
                .placeholder(R.mipmap.loading)
                .error(R.mipmap.default_head)
                .into(((ClassifyAdapter.HomeNotesHolder) holder).head_image);
    }

    @Override
    public int getItemCount() {
        return list_notes == null ? 0 : list_notes.size();
    }

    class HomeNotesHolder extends BaseRecyclerViewHolder implements View.OnClickListener{
        int position;
        private RelativeLayout note_item_view;
        private TextView name_tv, time_tv, hot_tv, classify_tv, follow_tv, title_tv, follow_number, reply_number, look_number, price_tv, total_num, ok_number;
        private ImageView head_image;
        private RecyclerView image_rv;
        private ImageAdapter imageAdapter;

        public HomeNotesHolder(View itemView) {
            super(itemView);
            note_item_view = (RelativeLayout) itemView.findViewById(R.id.note_item_view);
            ok_number = (TextView) itemView.findViewById(R.id.ok_number);
            name_tv = (TextView) itemView.findViewById(R.id.name_tv);
            time_tv = (TextView) itemView.findViewById(R.id.time_tv);
            hot_tv = (TextView) itemView.findViewById(R.id.hot_tv);
            classify_tv = (TextView) itemView.findViewById(R.id.classify_tv);
            follow_tv = (TextView) itemView.findViewById(R.id.follow_tv);
            title_tv = (TextView) itemView.findViewById(R.id.title_tv);
            follow_number = (TextView) itemView.findViewById(R.id.follow_number);
            reply_number = (TextView) itemView.findViewById(R.id.reply_number);
            look_number = (TextView) itemView.findViewById(R.id.look_number);
            price_tv = (TextView) itemView.findViewById(R.id.price_tv);
            total_num = (TextView) itemView.findViewById(R.id.total_num);
            head_image = (ImageView) itemView.findViewById(R.id.head_image);
            image_rv = (RecyclerView) itemView.findViewById(R.id.image_rv);
            LinearLayoutManager manager = new LinearLayoutManager(context);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            image_rv.setLayoutManager(manager);
            imageAdapter = new ImageAdapter(context, null);
            image_rv.setAdapter(imageAdapter);
            note_item_view.setOnClickListener(this);
            follow_tv.setOnClickListener(this);
            imageAdapter.setImageAdapterCall(new ImageAdapterCall() {
                @Override
                public void onClick(View view, int x) {
                    Gson gson = new Gson();
                    String json = gson.toJson(list_notes.get(position));
                    MyMethod.toActivity(context, NoteDetail.class, new String[]{"json"}, new String[]{json});
                }

                @Override
                public void onDetele(View view, int x) {

                }
            });
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.follow_tv:
                    //调用服务器接口--关注某人
                    follow_tv.setText("已关注");
                    break;
                case R.id.note_item_view:
                    //触发点击帖子事件
                    Gson gson = new Gson();
                    String json = gson.toJson(list_notes.get(position));
                    MyMethod.toActivity(context, NoteDetail.class, new String[]{"json"}, new String[]{json});
                    break;
            }
        }

    }
}
